/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#ifndef _INC_AK_TIMER_
#define	_INC_AK_TIMER_

#include "ak_queue.h"
#include "ak_lock.h"

#ifdef __cplusplus
extern "C" {
#endif

/* -----------------------------------------------------------------------------
** SoftTimer Define & OPs:
*/

/* TIMER Control  Param */
#define STIMER_RES			50			/* Ratio = TimerRes/1ms 	*/
#define STIMER_ENTRY 		0X400		/* 1024*50ms Entry, 51.2S/Cycle*/
#define STIMER_RING 		(STIMER_RES * STIMER_ENTRY)
#define STIMER_MOD 			SYS_MOD1K
#define STIMER_MAGIC 		0X6A7B8C9D

/* Add Support of Reduce Timer Excursion	*/
struct 	STimer_Svr;

typedef struct {
	_t_bnode 			node;
	struct STimer_Svr*	pSvr;

	unsigned short 		state;			/* timer slice state */
	unsigned short 		entry;			/* expire value (relative value) */
	unsigned int 		intv;
	unsigned int 		ring;
	unsigned int 		magic;
	unsigned long long	expire; 		/* expire value (absolute value) */
	_t_cbentry			cbEntry;
}STimer_Node;

typedef struct STimer_Svr{
	int					state;	
	unsigned long long	tick;			/* absolute value */		
	_t_sysCS 			lock;
	_t_blist			scheQ[STIMER_ENTRY];
}STimer_Svr;

extern 	int			STimer_SvrStart(STimer_Svr * pSvr);
extern 	int			STimer_SvrStop(STimer_Svr * pSvr);

extern	int 		STimer_Init(STimer_Node * pTimer, STimer_Svr * pSvr, _t_cbentry * pCbEntry, unsigned int intv);
extern	int			STimer_SetIntv(STimer_Node * pTimer, unsigned int intv);

extern 	int			STimer_Start(STimer_Node * pTimer);
extern 	int			STimer_Stop (STimer_Node * pTimer);
extern	int			STimer_StartEx(STimer_Node * pTimer, unsigned int diff);

extern	int 		STimer_Sche(STimer_Svr * pSvr, int  scheCnt);


#ifdef __cplusplus
}
#endif

#endif

